<template>
	<view class="warp">
		<uni-segmented-control :current="current" :values="localSeg" @clickItem="segClick" styleType="text"></uni-segmented-control>
		<uni-list>
			<uni-list-item v-for="item in dataList" :key="item._id"
			:thumb="item.userInfo.avatar_file?.url || '../../static/logo.png' " thumbSize="sm" :right-text="item.number + ' 玫瑰'"
			:title="item.userInfo.nickname || '匿名'" :note="item.message"  @click="navigator(item.userInfo._id)" clickable></uni-list-item>
			<!-- <uni-list-item title="" note=""></uni-list-item> -->
		</uni-list>
		<uni-load-more :status="more"></uni-load-more> 
	</view>
</template>

<script>
	import{ mapState }from 'pinia'
	import{ useUserStore } from "@/pinia/user.js"
	const db = uniCloud.database();
	
	export default {
		name:"wolf-share-header",
		data() {
			return {
				localSeg:['送我的','我送'],
				current:0,
				dataList:[],
				more:"more",
				skip:0,//默认跳过0
				limit:20,//每次返回20条
			}
		},
		computed:{
			...mapState(useUserStore,['userInfo'])
		},
		onLoad(){
			this.load()
		},
		methods: {
			async load(){
				let where = `pid== '${this.userInfo._id}'`
				if(this.current==1){
					where = `uid== '${this.userInfo._id}'`
				}
				let res = db.collection("wolf-gift-log").where(where).orderBy('create_time desc').getTemp()
				let user = db.collection('uni-id-users').field('nickname,avatar_file,_id').getTemp()
				let gift_log = await db.collection(res,user).skip(this.skip).limit(this.limit).get({
					getCount:true
				})
				gift_log.result.data.forEach(item=>{
					if(this.current){
						item.userInfo = item.pid[0]//我送给谁的
					}else{
						item.userInfo = item.uid[0]
					}
				})
				this.dataList = [...this.dataList,...gift_log.result.data]
				this.skip += this.limit
				if(this.skip > gift_log.result.count){
					this.more = "no-more"
				}else{
					this.more = "more"
				}
				console.log("gift_log",gift_log);
			},
			clear(){
				this.dataList = []
				this.skip = 0
				this.limit = 20
			},
			segClick(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
					this.clear()
					this.load()
				}
			},
			navigator(id){
				console.log("id==>",id);
				if(!id) return
				uni.navigateTo({
					url:`/pages/list/userInfo?id=${id}`
				})
			}
		},
		onReachBottom() {
			if(this.more=="more"){
				this.more = 'loading'
				this.load()
			}
		}
	}
</script>

<style scoped lang="scss">
	view{
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
	}
</style>
